/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;

/**
 *
 * @author Welington
 */
public class CustomProcess {

    private int id;
    private String label;
    private String host;
    private int port;
    private SocketAddress address;
    private Debug debug = Debug.getInstance();
    private Class current = CustomProcess.class;

    public CustomProcess(int id, String label, String host, int port) {
        this.id = id;
        this.label = label;
        this.host = host;
        this.port = port;
    }

    public CustomProcess() {
        this.id = -1;
        this.label = null;
        this.host = null;
        this.port = -1;
    }

    public String getHost() {
        if (host != null) {
            return host;
        } else {
            debug.print(Debug.ERROR, current, "Trying to recover process attribute (Host) not initialized");
        }

        return "";
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        if (port > -1) {
            return port;
        } else {
            debug.print(Debug.ERROR, current, "Trying to recover process attribute (Port) not initialized");
        }
        
        return -1;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public SocketAddress getAddress() {
        SocketAddress socketAddress = null;
        
        if (host != null && port > -1) {
            try {
                InetAddress inetAddress = InetAddress.getByName(host);
                socketAddress = new InetSocketAddress(inetAddress, getPort());

            } catch (UnknownHostException ex) {
                debug.print(Debug.ERROR, current, ex.getLocalizedMessage());
            }
        } else {
            debug.print(Debug.ERROR, current, "Trying to recover process attribute (Address) not initialized");
        }
        
        return socketAddress;
    }

    public int getId() {
        if (id > -1) {
            return id;
        } else {
            debug.print(Debug.ERROR, current, "Trying to recover process attribute (Id) not initialized");
        }
        
        return -1;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLabel() {
        if (label != null) {
            return label;
        } else {
            debug.print(Debug.ERROR, current, "Trying to recover process attribute (Label) not initialized");
        }
        
        return "";
    }

    public void setLabel(String label) {
        this.label = label;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o != null && o instanceof CustomProcess) {
            CustomProcess other = (CustomProcess) o;
            if (this.id == other.getId())
                return true;
            
            return false;
        }
        
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + this.id;
        return hash;
    }
}
